import xEvent from "./xEvent.js";
import xAlert from "./xAlert.js";
/*这里放置共享数据管理
 * 不变化的
 * 
 * 单实例*/
export default class xShare {

	share = {};

	static getInstance() {
		if(!this.instance) {
			this.instance = new xShare();
		}
		return this.instance;
	}

	static getData(key) {
		let instance = xShare.getInstance();
		if(key)
			return instance.share[key]
        else
            return null;
	}
	
	static getApiData(key,api,param){
		let instance = xShare.getInstance();
		if(instance.share[key]){
            xEvent.DataReady(key,instance.share[key]);
		}else{
            api(param,res => {
                if(res.code ==200){
                    xShare.put(key,data);
                    xEvent.DataReady(key,res.data);
                }else
                    xAlert.error("数据获取出错",2000);
            })
		}
		
	}

	static putData(key,data){
		let instance = xShare.getInstance();
		instance.share[key] = data;
	}
}